package com.ttpai.boot.lbs.service;

import com.ttpai.boot.lbs.dao.RoutePlanReportDao;
import com.ttpai.boot.lbs.model.RoutePlanReportVO;
import com.ttpai.boot.lbs.model.assist.GroupByVO;
import com.ttpai.boot.lbs.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by kail on 2017/2/26.
 */
@Service
public class RoutePlanReportService {

    @Autowired
    private RoutePlanReportDao routePlanReportDao;


    /**
     * 获取所有的天数，不管有没有抓取经纬度
     */
    public List<GroupByVO> selectHaveLLDayGroupByAll() {
        return routePlanReportDao.selectHaveLLDayGroupByAll();
    }

    /**
     * 获取已经抓取过经纬度日期
     */
    public List<GroupByVO> selectHaveLLDayNotZero() {
        List<GroupByVO> groupByVOS = this.selectHaveLLDayGroupByAll();
        List<GroupByVO> notZero = new ArrayList<GroupByVO>();
        for (GroupByVO groupByVO : groupByVOS) {
            if (groupByVO.getCount() > 0) {
                notZero.add(groupByVO);
            }
        }
        return notZero;
    }

    /**
     * 查询某一天的所有信息
     */
    public List<RoutePlanReportVO> selectRoutePlanReportByDate(Date date) {
        return routePlanReportDao.selectRoutePlanReportByDate(date);
    }

    /**
     * 查询某一天的所有信息
     */
    public List<RoutePlanReportVO> selectRoutePlanReportByDate(Date date, String cityName) {
        RoutePlanReportVO reportVO = new RoutePlanReportVO();
        reportVO.setCheckTime(date);
        reportVO.setBranchCityName(cityName);
        return routePlanReportDao.selectRoutePlanReport(reportVO);
    }

    public Map<String, List<RoutePlanReportVO>> showCheckedLine(List<String> lines, Map<String, List<RoutePlanReportVO>> route, Integer hideOtherLine) {
        Map<Long, RoutePlanReportVO> dandian = new HashMap<>();
        for (Map.Entry<String, List<RoutePlanReportVO>> entry : route.entrySet()) {
            List<RoutePlanReportVO> value = entry.getValue();
            for (RoutePlanReportVO r : value) {
                dandian.put(r.getId(), r);
            }
        }

        Map<String, List<RoutePlanReportVO>> returnRoute = new LinkedHashMap<>();
        for (String line : lines) {
            for (Map.Entry<String, List<RoutePlanReportVO>> entry : route.entrySet()) {
                String idStr = "," + entry.getValue().get(0).getId().toString() + ",";
                if (line.startsWith(idStr)) {
                    returnRoute.put(UUID.randomUUID().toString(), entry.getValue());
                    for (RoutePlanReportVO r : entry.getValue()) {
                        dandian.remove(r.getId());
                    }
                }
            }
        }

        if (null != hideOtherLine && 1 == hideOtherLine) {
            Set<Map.Entry<Long, RoutePlanReportVO>> entries = dandian.entrySet();
            for (Map.Entry<Long, RoutePlanReportVO> entry : entries) {
                returnRoute.put(UUID.randomUUID().toString(), Arrays.asList(entry.getValue()));
            }
        }

        return returnRoute;
    }

    public Map<String, List<RoutePlanReportVO>> selectCurrentRoutePlan(Date date, String city) {
        List<RoutePlanReportVO> allReports = this.selectRoutePlanReportByDate(date, city);

        Map<String, List<RoutePlanReportVO>> route = new HashMap<>();
        for (RoutePlanReportVO report : allReports) {
            if (null == report.getLng() || report.getLng().equals(0D)) {
                continue;
            }

            String checkAdminName = report.getCheckAdminName();
            if (StringUtils.hasText(checkAdminName)) {
                List<RoutePlanReportVO> vos = route.get(checkAdminName);
                if (null == vos) {
                    vos = new ArrayList<>();
                    route.put(checkAdminName, vos);
                }
                vos.add(report);
            } else {
                route.put(UUID.randomUUID().toString(), Arrays.asList(report));
            }
        }
        return route;
    }


    /**
     * 更新数据
     */
    public Integer updateRoutePlanReportNotNull(RoutePlanReportVO report) {
        return routePlanReportDao.updateRoutePlanReportNotNull(report);
    }


}
